from django.contrib import admin

from .models import *


class BedTypeAdmin(admin.ModelAdmin):
    list_display = list_display_links = ['id', 'type']


admin.site.register(BedType, BedTypeAdmin)


@admin.register(BuildManage)
class BuildManageAdmin(admin.ModelAdmin):
    def make_published(self, request, queryset):
        for item in queryset:
            for floor in range(1, item.number_of_floors + 1):
                for room in range(1, item.number_of_room_per_floor + 1):
                    name = item.build_name + str(floor) + str(room).rjust(2, '0')
                    number = item.bed_type.number
                    bed_type = item.bed_type
                    build_id = item
                    Room.objects.update_or_create(
                        build_id=build_id,
                        name=name,
                        defaults={
                            'build_id': build_id,
                            'name': name,
                            'number': number,
                            'bed_type': bed_type,
                        }
                    )

    make_published.short_description = "生成/更新-楼宇寝室号"

    list_display = list_display_links = ['id', 'build_name', 'bed_type', 'detail']
    actions = [make_published]


@admin.register(Room)
class RoomManageAdmin(admin.ModelAdmin):
    list_display = list_display_links = ['name', 'bed_type', 'number', 'actual_number']
    list_filter = ['build_id']
    search_fields = ['name']
